package com.itheima.tcp.fileupload.demo1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServiceDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器socket对象，监听10000端口
        ServerSocket ss = new ServerSocket(10000);

        //监听客户端连接
        Socket accept = ss.accept();

        //通过accept获取输入流对象
        InputStream inputStream = accept.getInputStream();
        //创建本地输出流对象，将文件写入磁盘
        FileOutputStream fos = new FileOutputStream("E:\\filedemo\\input\\a.jpg");
        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }

        //反馈数据
        OutputStream outputStream = accept.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
        bw.write("上传成功");
        bw.newLine();
        bw.flush();

        fos.close();
        accept.close();
        ss.close();
    }
}
